<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include("menu.php"); ?>

<script language = "JavaScript">

		//**** List Amphur (Start) ***//
		function ListSub(SelectValue)
		{
			document.formadd.category_sub_id.length = 0;

			//*** Insert null Default Value ***//
			var myOption = new Option('','');

			
			<?php
			$strSQL = "SELECT * FROM category_sub ORDER BY category_sub_name ASC";
			$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
			$intRows = 0;
			while($objResult = mysql_fetch_array($objQuery))
			{
			$intRows++;
			?>
				x = <?php echo $intRows; ?>;
				mySubList = new Array();
				
				strGroup = <?php echo $objResult["category_id"];?>;
				strValue = "<?php echo $objResult["category_sub_id"];?>";
				strItem = "<?php echo $objResult["category_sub_name"];?>";
				mySubList[x,0] = strItem;
				mySubList[x,1] = strGroup;
				mySubList[x,2] = strValue;
							
				if (mySubList[x,1] == SelectValue){
					var myOption = new Option(mySubList[x,0], mySubList[x,2])  ;
					document.formadd.category_sub_id.options[document.formadd.category_sub_id.length]= myOption		;			
				}
			<?php
			}
			?>																
		}
		//**** List Amphur (End) ***//


	function setFocus(){
		document.formadd.name.focus();
	}
</script>
</head>
<body onload="setFocus()">
<?php

if($_GET["action"]==add) {

	if($_POST["name"]!="" && $_POST["category_id"]!="" && $_POST["category_sub_id"]!="" && $_POST["unit_id"]!="")
		{
		
		$checkcode = "SELECT product_name FROM product WHERE product_name = '".$_POST["name"]."' ";
		$result = mysql_query($checkcode) or die ("'".checkcode."'");
		$row = mysql_num_rows($result);
		
		if($row>=1){
			$save="ชื่อสินค้า ".$_POST["name"]." นี้มีอยู่แล้วกรุณาใช้ชื่ออื่น";
			} else {
			$Query = "INSERT INTO product (product_name,category_id,category_sub_id,unit_id) VALUES ";
			$Query .="('".$_POST["name"]."','".$_POST["category_id"]."','".$_POST["category_sub_id"]."','".$_POST["unit_id"]."' )";
			$objQuery = mysql_query($Query);

			if($objQuery) {
				$save="บันทึกข้อมูลเรียบร้อยแล้ว";
				} else {
				echo "Error Save [".$Query."]";
				}
			}
		} else {
		$save="กรุณากรอกข้อมูลให้ครบ";
		}
}
?>
<form action="?action=add" name="formadd" method="post" id="formadd">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>เพิ่มสินค้า</h1>
    <div class="buttons"><a onclick="formadd.submit();" class="button"><span>บันทึก</span></a><a onclick="location = 'product_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form" id="form">
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input type="text" name="name" id="name" size="30" /></td>
      </tr>
      <tr>
        <td>หน่วยนับ : </td>
        <td><select name="unit_id" id="unit_id" >
                <option value=""><-- Please Select --></option>
                <?php
                $Querycate = "SELECT * FROM unit ORDER BY unit_name ASC";
                $objQuerycate = mysql_query($Querycate);
                while($row = mysql_fetch_array($objQuerycate))
                {
			?>
                <option value="<?php echo $row["unit_id"];?>">
                <?php echo "  ".$row["unit_name"];?>
                </option>
                <?php
			}
			?>
              </select></td>
      </tr>
      <tr>
        <td>หมวดสินค้า : </td>
        <td><select name="category_id" id="category_id" onChange = "ListSub(this.value)" >
                <option value=""><-- Please Select --></option>
                <?php
			$Querycate = "SELECT * FROM category ORDER BY category_name ASC";
			$objQuerycate = mysql_query($Querycate);
			while($row = mysql_fetch_array($objQuerycate))
			{
			?>
                <option value="<?php echo $row["category_id"];?>">
                <?php echo "  ".$row["category_name"];?>
                </option>
                <?php
			}
			?>
              </select></td>
      </tr>
      <tr>
        <td>หมวดสินค้าย่อย : </td>
        <td><select name="category_sub_id" id="category_sub_id" style="min-width:140px;" ><option value=""><-- Please Select --></option></select></td>
      </tr>
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</div>
</form>
</body>
</html>
